package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.GenreDao;
import com.fduss.artStore.entity.Gallery;
import com.fduss.artStore.entity.Genre;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class GenreDaoImpl implements GenreDao {

    private JdbcUtil util;

    public GenreDaoImpl(){
        util = new JdbcUtil();
    }


    @Override
    public Genre add(Genre genre) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`genre` " +
                "(`GenreID`, `GenreName`, `Era`, `Description`, " +
                "`Link`) VALUES (?, ?, ?, ?, ?)";

        PreparedStatement pst = null;
        boolean flag = false;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, genre.getGenreId());
            pst.setString(2, genre.getGenreName());
            pst.setInt(3, genre.getEra());
            pst.setString(4, genre.getDescription());
            pst.setString(5, genre.getLink());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);

        }

        return flag ? genre : null;
    }

    @Override
    public void delete(int genreId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM genre WHERE `GenreID` = ?";
        PreparedStatement pst = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, genreId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public Genre getOne(int genreId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`genre` WHERE `GenreID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        Genre genre= null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, genreId);
            rs = pst.executeQuery();

            if (rs.next()){
                genre = buildGenre(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }

        return genre;
    }

    @Override
    public Genre getByArtwork(int artworkId) {
        //todo :
        return null;
    }

    @Override
    public List<Genre> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`genre`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Genre> genre = new ArrayList<>();

        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()){
                genre.add(buildGenre(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }

        return genre;
    }

    private Genre buildGenre(ResultSet rs) throws SQLException {
        Genre genre = new Genre();
        genre.setDescription(rs.getString("Description"));
        genre.setEra(rs.getInt("Era"));
        genre.setGenreId(rs.getInt("GenreID"));
        genre.setGenreName(rs.getString("GenreName"));
        genre.setLink(rs.getString("Link"));

        return genre;
    }
}
